{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pandas 进阶修炼 ｜早起Python\n",
    "<br>\n",
    "\n",
    "**本习题由公众号【早起Python & 可视化图鉴】 原创，转载及其他形式合作请与我们联系（微信号`sshs321`)，未经授权严禁搬运及二次创作，侵权必究！**\n",
    "\n",
    "\n",
    "\n",
    "本习题基于 `pandas` 版本 `1.1.3`，所有内容应当在 `Jupyter Notebook` 中执行以获得最佳效果。\n",
    "\n",
    "不同版本之间写法可能会有少许不同，如若碰到此情况，你应该学会如何自行检索解决。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2 - pandas 个性化显示设置\n",
    "<br>\n",
    "\n",
    "在使用 `pandas` 时，有时默认的配置方案并不能让我们舒服的进行数据分析。\n",
    "\n",
    "幸运的是，`pandas` 也支持我们 <font color=#E36C07>**自定义显示、样式等个性化操作**</font>。\n",
    "\n",
    "本节将部分常用的设置整理为习题形式，<font color=#E36C07>  **所有操作答案拿走即用。既可以刷一遍来了解有这样那样的设置，也可以保存用于速查手册** </font>  \n",
    "\n",
    "注意：本习题中未提及的配置可以点击查阅 `pandas` 👉 [**官方文档对应文章**](https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html)\n",
    "\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化\n",
    "\n",
    "<br>\n",
    "\n",
    "该 `Notebook` 版本为**纯习题版**\n",
    "\n",
    "如果需要答案或者提示，可以微信搜索公众号「早起Python」获取！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 加载数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入 `pandas` 并读取当前目录下 `csv` 数据(`data.csv`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"data.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2-1 基于 option 修改显示设置\n",
    "<br>\n",
    "\n",
    "在 `pandas` 中有一个 `option` 系统，可以通过 `set_option`方法进行进阶显示选项设置。\n",
    "\n",
    "本小节主要整理了一些基于 `option` 修改数据显示的设置。\n",
    "\n",
    "注意【**基于 option 修改显示设置**】并未修改数据，仅是在原有数据基础上优化显示状态，随时可以通过重置选项重置全部设置，恢复数据默认显示状态。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1 显示全部列\n",
    "\n",
    "<br>\n",
    "如下图所示👇，直接查看 `data` 会发现，由于数据维度较大，部分行列会被折叠，显示为`...`，现在需要显示全部的列方便预览。\n",
    "\n",
    "![](http://liuzaoqi.oss-cn-beijing.aliyuncs.com/2021/08/19/16293397012946.jpg?域名/sample.jpg?x-oss-process=style/stylename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在下面的 cell 中输入你的解决方案，并在最后执行 `data.head()`以检查你的答案是否正确解决问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2 显示指定行/列\n",
    "\n",
    "<br>\n",
    "\n",
    "指定让 `data` 在预览时显示10列，7行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3 还原行/列显示数\n",
    "\n",
    "<br>\n",
    "还原上面的显示设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4 修改每列最大字符宽度\n",
    "\n",
    "<br>\n",
    "\n",
    "即每列最多显示的字符长度，例如【每列最多显示10个字符，多余的会变成`...`】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5 修改小数点精度\n",
    "\n",
    "<br>\n",
    "\n",
    "修改默认显示精度为小数点后5位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6 还原所有显示设置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还原上面的全部显示设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2-2 更多 option 相关设置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7 忽略警告\n",
    "<br>\n",
    "\n",
    "取消`pandas`相关`warning`提示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8 设置数值显示条件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果数值小于 20 则显示为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9 让 pandas 支持 LaTex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "让`dataframe`中内容支持 `Latex` 显示（需要使用`$$`包住）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10 修改默认绘图引擎\n",
    "\n",
    "修改`pandas`默认绘图引擎为`plotly`（需要提前安装好`plotly`）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 11 还原所有 option 设置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还原上面全部 option 设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 彩蛋"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如何设置在预览数据时，不换行显示每列内容？\n",
    "\n",
    "![](http://liuzaoqi.oss-cn-beijing.aliyuncs.com/2021/08/19/16293394983242.jpg?域名/sample.jpg?x-oss-process=style/stylename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2-3 基于 style  个性化设置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面基于 `option` 的 `pandas` 相关设置是<font color=#E36C07>全局配置</font>，一次设置会在关闭notebook前一直有效\n",
    "\n",
    "但相关常用的设置并不多，不能满足更多的个性化需求。\n",
    "\n",
    "幸运的是在 `pandas` 中提供 `Styler` 对象让我们进一步个性化展示数据。\n",
    "\n",
    "本节我就将一些常用的基于 `style` 个性化设置整理为习题模式方便大家学习、巩固。\n",
    "\n",
    "注意：基于 `style` 个性化设置<font color=#E36C07>**同样不会修改数据**</font>，所有 `data.style.xxxx` 输出的数据均是<font color=#E36C07>一次性的（可以复用、导出）</font>，因此你应该在合适的时间选择使用该方法。\n",
    "\n",
    "下面仅列举常用的方法，若想了解更多可以查阅[**pandas官方文档对应文章👉**](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  重新加载数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为了方便理解，重新读取`data.csv`**前20行指定列**\n",
    "- `'positionName'`\n",
    "- `'createTime'`（设置为时间格式）\n",
    "- `'salary'`\n",
    "- `'subwayline'`\n",
    "- `'matchScore'`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"data.csv\", usecols=[\n",
    "                   'positionName', 'createTime', 'salary', 'subwayline', 'matchScore'], nrows=20, parse_dates=['createTime'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 12 隐藏索引\n",
    "\n",
    "<br>\n",
    "\n",
    "隐藏索引列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 13 调整精度\n",
    "\n",
    "<br>\n",
    "\n",
    "将带有小数点的列精度调整为小数点后2位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 14 标记缺失值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将缺失值标记为`数据缺失`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "微信搜索公众号「早起Python」，关注后可以获得更多资源！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 15 高亮缺失值\n",
    "\n",
    "<br>\n",
    "\n",
    "将缺失值高亮，颜色名`skyblue`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 16 高亮数值列最大值\n",
    "<br>\n",
    "将 数值格式列的最大值进行高亮"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 17 高亮数值列最小值\n",
    "<br>\n",
    "将 数值格式列的最小值进行高亮"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 18 同时高亮最大最小值\n",
    "\n",
    "<br>\n",
    "\n",
    "同时高亮最大值（颜色代码为`#F77802`）与最小值（颜色代码为`#26BE49`）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 19 指定格式高亮"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "高亮 `salary` 列范围在 3000 - 10000 的数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 20 渐变显示数值列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将数值格式的列使用渐变色（绿色）进行显示，以突出趋势"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 21 修改字体颜色\n",
    "\n",
    "<br>\n",
    "\n",
    "将 `salary` 列修改为红色字体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 22 修改背景颜色、对齐方式、字体大小\n",
    "<br>\n",
    "\n",
    "将整个 `dataframe` 进行如下设置：\n",
    "- 居中\n",
    "- 背景色修改为 `#F8F8FF`\n",
    "- 字体:13px"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 23 综合(链式)设置\n",
    "<br>\n",
    "\n",
    "除了上面的单个设置，还可以将多个设置进行结合，下面对整个 `dataframe` 进行如下设置：\n",
    "- 居中\n",
    "- 背景色修改为 `#F8F8FF`\n",
    "- 字体:13px\n",
    "\n",
    "并将 `salary` 列字体修改为红色"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 24 导出样式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将上一题带有样式的 `pandas` 数据框导出为本地 Excel(`.xlsx`格式)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 25 制作指定列条形图\n",
    "\n",
    "<br>\n",
    "\n",
    "在 `pandas` 中对 `salary` 列使用条形图进行可视化，指定颜色`skyblue`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 26 带有条件的样式（自定义样式）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将 `salary` 列数值大于 30000 的单元格字体修改为红色"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 27 格式化输出日期类型\n",
    "\n",
    "<br>\n",
    "\n",
    "将 `createTime` 列格式化输出为 `xx年xx月xx日` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 28 指定（自定义）格式化数据\n",
    "\n",
    "<br>\n",
    "\n",
    "- 在 `salary` 列后增加\"元\"\n",
    "- 对 `matchScore` 列保留两位小数并增加\"分\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](http://liuzaoqi.oss-cn-beijing.aliyuncs.com/2021/09/16/16317972442543.jpg?域名/sample.jpg?x-oss-process=style/stylename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "288px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
